---@class RedRob.Stone : RedRob.Pawn
---@field New fun():RedRob.Stone
local Stone = RedRob.CreateClass("Stone","Pawn")
function Stone:OnCreate()
    self.modelId = 0
    self.modelName = ""
    self.modelObj = nil
    self.loadRequestId = 0
    ---@type Spine.Unity.SkeletonAnimation
    self.animation = nil
    ---@type UnityEngine.Renderer[]
    self.renders = nil
    self.isLoading = false
    self.isLoaded = false
    self.hp = 0
    self.hpMax = 0
    self.status = RedRob.Config.MineralStatus.full
    self.needPlaySound = false
    self.isCanDestroyed = false
end
function Stone:GetModelId()
    return self.modelId
end
function Stone:SetModelId(modelId)
    self.modelId = modelId
    local cfg = dataTable.getDataTableByStringId("t_darkstreetstone",self.modelId)
    if not cfg then
        return
    end
    self.modelName = cfg.f_ModelPath
end
function Stone:OnDestroy()
    utimer.remove(self.timerAnimId)
    utimer.remove(self.destroyTimerId)

    RedRob.PoolMgr.CancelLoad(self.loadRequestId)
    RedRob.PoolMgr.ReturnPrefabToPool(self.modelName,self.modelObj)
    self.modelObj = nil
    self.loadRequestId = 0
    self.destroyTimerId = nil
end
function Stone:LoadModel()
    if self.isLoading then
        return
    end
    self.isLoading = true
    if string.IsEmpty(self.modelName) then return end
    self.loadRequestId = RedRob.PoolMgr.LoadPrefabFromPool(self.modelName,self.LoadCallback,self)
end
function Stone:LoadCallback(go)
    self.modelObj = go
    self.modelObj.transform:SetParent(self.transform,false)
    Util.Lua_SetLocalPositionXYZ(self.modelObj,0,0,0)
    self.animation = self.modelObj:GetComponentInChildren(TSkeletonAnimation)
    self.renders = self.gameObject:GetComponentsInChildren(typeof(UnityEngine.Renderer))
    self:InitObject()
end
---@private
function Stone:InitObject()
    self.gameObject.name = "s"..self.id
    Util.Lua_SetLocalPositionXYZ(self.transform,self.pos.x,self.pos.y,self.pos.z)
    Util.Lua_SetLocalScaleXYZ(self.transform,1,1,1)
    if self.animation then
        Util.PlayAction(self.animation.gameObject,"idle1")
        Util.Lua_SetLocalRotationXYZ(self.animation.transform,0,0,0)
    end
    self.boxCollider.size = Vector3(1.5,1.5,0.01)
    self.boxCollider.center = Vector3(0,0,0)
    FireEvent(RedRob.Config.Event.ActorPosChanged,self)
end

--被打开
function Stone:IsOpened()
    return self.status == RedRob.Config.MineralStatus.digEmpty
end
function Stone:SetSortingOrder(order)
    self.sortingOrder = order
    if not self.renders then return end
    if self:IsDead() then return end
    if not RedRob.CameraController.IsPlayerInScreen(self) then
        return
    end
    local length = self.renders.Length
    for i = 0 , length - 1 do
        self.renders[i].sortingOrder = order * self.orderStep + i
    end
end

function Stone:GetType()
    return self.modelId-1
end

function Stone:GetHp()
    return self.hp
end
function Stone:GetHpMax()
    return self.hpMax
end
function Stone:Hurt(hp,attackPlayer)
    if self.isDead then return end
    hp = math.max(0,self.hp - hp)
    self.hp = hp
    FireEvent(RedRob.Config.Event.PlayerHpChanged,self,attackPlayer)
    if self.hp <= 0 then
        self:SetDead()
        local time = Util.PlayAction(self.animation.gameObject,"idle2")
        if time > 0 then
            self.destroyTimerId = utimer.timeCall(self.DestroyAnimFinished,time*1000,1,self)
        else
            self.isCanDestroyed = true
        end
        FireEvent(RedRob.Config.Event.ActorDead,self,attackPlayer)
    else
        FireEvent(RedRob.Config.Event.PlayerHpChanged,self,attackPlayer)
    end
end
function Stone:DestroyAnimFinished()
    self.destroyTimerId = nil
    self.isCanDestroyed = true
    RedRob.ActorManager.DestroyActor(self)
end